import request from '@/utils/request'

// ==================== 商品管理 ====================

// 分页查询商品
export function getProductPage(params) {
  return request({
    url: '/v1/products',
    method: 'get',
    params
  })
}

// 获取商品详情
export function getProductDetail(id) {
  return request({
    url: `/v1/products/${id}`,
    method: 'get'
  })
}

// 创建商品
export function createProduct(data) {
  return request({
    url: '/v1/products',
    method: 'post',
    data
  })
}

// 更新商品
export function updateProduct(id, data) {
  return request({
    url: `/v1/products/${id}`,
    method: 'put',
    data
  })
}

// 删除商品
export function deleteProduct(id) {
  return request({
    url: `/v1/products/${id}`,
    method: 'delete'
  })
}

// 上架商品
export function publishProduct(id) {
  return request({
    url: `/v1/products/${id}/publish`,
    method: 'post'
  })
}

// 下架商品
export function unpublishProduct(id) {
  return request({
    url: `/v1/products/${id}/unpublish`,
    method: 'post'
  })
}

// 更新商品状态
export function updateProductStatus(id, status) {
  return request({
    url: `/v1/products/${id}/status`,
    method: 'put',
    data: { status }
  })
}

// 批量导入商品
export function importProducts(data) {
  return request({
    url: '/v1/products/import',
    method: 'post',
    data
  })
}

// 导出商品
export function exportProducts(params) {
  return request({
    url: '/v1/products/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// ==================== 订单管理 ====================

// 分页查询订单
export function getOrderPage(params) {
  return request({
    url: '/v1/orders',
    method: 'get',
    params
  })
}

// 获取订单详情
export function getOrderDetail(id) {
  return request({
    url: `/v1/orders/${id}`,
    method: 'get'
  })
}

// 更新订单状态
export function updateOrderStatus(id, status, note) {
  return request({
    url: `/v1/orders/${id}/status`,
    method: 'put',
    params: { status, note }
  })
}

// 发货
export function shipOrder(id, data) {
  return request({
    url: `/v1/orders/${id}/ship`,
    method: 'post',
    data
  })
}

// 删除订单
export function deleteOrder(id) {
  return request({
    url: `/v1/orders/${id}`,
    method: 'delete'
  })
}

// 取消订单
export function cancelOrder(id, reason) {
  return request({
    url: `/v1/orders/${id}/cancel`,
    method: 'post',
    params: { reason }
  })
}

// 退款
export function refundOrder(id, data) {
  return request({
    url: `/v1/orders/${id}/refund`,
    method: 'post',
    data
  })
}

// 导出订单
export function exportOrders(params) {
  return request({
    url: '/v1/orders/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// ==================== 支付管理 ====================

// 分页查询支付记录
export function getPaymentPage(params) {
  return request({
    url: '/v1/payments',
    method: 'get',
    params
  })
}

// 获取支付详情
export function getPaymentDetail(id) {
  return request({
    url: `/v1/payments/${id}`,
    method: 'get'
  })
}

// 获取支付统计
export function getPaymentStatistics() {
  return request({
    url: '/v1/payments/statistics',
    method: 'get'
  })
}

// 退款支付
export function refundPayment(id, data) {
  return request({
    url: `/v1/payments/${id}/refund`,
    method: 'post',
    data
  })
}

// 确认支付
export function confirmPayment(id) {
  return request({
    url: `/v1/payments/${id}/confirm`,
    method: 'post'
  })
}

// 处理退款
export function processRefund(id, data) {
  return request({
    url: `/v1/payments/${id}/refund`,
    method: 'post',
    data
  })
}

// 导出支付记录
export function exportPayments(params) {
  return request({
    url: '/v1/payments/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// ==================== 库存管理 ====================

// 分页查询库存
export function getInventoryPage(params) {
  return request({
    url: '/v1/inventory',
    method: 'get',
    params
  })
}

// 更新库存
export function updateInventory(productId, data) {
  return request({
    url: `/v1/inventory/${productId}`,
    method: 'put',
    data
  })
}

// 批量更新库存
export function batchUpdateInventory(data) {
  return request({
    url: '/v1/inventory/batch',
    method: 'put',
    data
  })
}

// 获取库存预警
export function getInventoryAlerts(params) {
  return request({
    url: '/v1/inventory/alerts',
    method: 'get',
    params
  })
}

// ==================== 商品分类管理 ====================

// 获取分类列表
export function getProductCategoryList() {
  return request({
    url: '/v1/product-categories',
    method: 'get'
  })
}

// 获取分类树
export function getProductCategoryTree() {
  return request({
    url: '/v1/product-categories/tree',
    method: 'get'
  })
}

// 创建分类
export function createProductCategory(data) {
  return request({
    url: '/v1/product-categories',
    method: 'post',
    data
  })
}

// 更新分类
export function updateProductCategory(id, data) {
  return request({
    url: `/v1/product-categories/${id}`,
    method: 'put',
    data
  })
}

// 删除分类
export function deleteProductCategory(id) {
  return request({
    url: `/v1/product-categories/${id}`,
    method: 'delete'
  })
}

// ==================== 优惠券管理 ====================

// 分页查询优惠券
export function getCouponPage(params) {
  return request({
    url: '/v1/coupons',
    method: 'get',
    params
  })
}

// 创建优惠券
export function createCoupon(data) {
  return request({
    url: '/v1/coupons',
    method: 'post',
    data
  })
}

// 更新优惠券
export function updateCoupon(id, data) {
  return request({
    url: `/v1/coupons/${id}`,
    method: 'put',
    data
  })
}

// 删除优惠券
export function deleteCoupon(id) {
  return request({
    url: `/v1/coupons/${id}`,
    method: 'delete'
  })
}

// 启用/禁用优惠券
export function toggleCouponStatus(id, enabled) {
  return request({
    url: `/v1/coupons/${id}/toggle`,
    method: 'post',
    params: { enabled }
  })
}
